package com.umerclass.ui.anmo;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.umerclass.R;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.okhttp.OkSimpleRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.utils.Constants;
import butterknife.BindView;
import butterknife.OnClick;

public class FindPasswordActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.linear_one)
    LinearLayout linearOne;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.linear)
    LinearLayout linear;
    @BindView(R.id.et_new_password_again)
    EditText etNewPasswordAgain;
    @BindView(R.id.image_again)
    ImageView imageAgain;
    @BindView(R.id.linear_again)
    LinearLayout linearAgain;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.linear_two)
    LinearLayout linearTwo;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;

    private int type = 1;//忘记密码  2提现密码忘记
    private String phone = "";
    private String code = "";
    private boolean click = false;
    CountDownTimer timer1 = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvGetCode.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tvGetCode.setText("重新获取验证码");
        }
    };

    private String password1 = "";
    private String password2 = "";
    private boolean click2 = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer1.cancel();
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_find_password;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));

        type = super.getIntent().getIntExtra("type", 1);
        if (type == 1) {
            tvTitle.setText("忘记密码");
            tvOne.setText("密码重置");
            tvTwo.setText("密码重置");
        }else if(type == 2){
            tvTitle.setText("忘记提现密码");
            tvOne.setText("提现密码重置");
            tvTwo.setText("提现密码重置");
        }
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    phone = "";
                } else {
                    phone = s.toString();
                }
                clickOne();
            }
        });

        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    code = "";
                } else {
                    code = s.toString();
                }
                clickOne();
            }
        });

        etNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    password1 = "";
                } else {
                    password1 = s.toString();
                }
                clickTwo();
            }
        });

        etNewPasswordAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    password2 = "";
                } else {
                    password2 = s.toString();
                }
                clickTwo();
            }
        });

    }

    @Override
    public void loadData() {

    }

    private boolean isCheck1 = false;
    private boolean isCheck2 = false;

    @OnClick({R.id.linear_back, R.id.tv_get_code, R.id.btn1, R.id.btn2,R.id.image_again,R.id.image})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.image:
                if(isCheck1){
                    isCheck1 = false;
                    etNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }else {
                    isCheck1 = true;
                    etNewPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.image_again:
                if(isCheck2){
                    isCheck2 = false;
                    etNewPasswordAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }else {
                    isCheck2 = true;
                    etNewPasswordAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_get_code:
                if (!TextUtils.isEmpty(etPhone.getText().toString())) {
                    timer1.start();
                    OkSimpleRequest request = new OkSimpleRequest<>(R.id.authPhone,
                            Constants.authPhone);
                    request.addParams("phone", phone);
                    requestOkhttpSimple(request);
                }else {
                    showToast("请输出手机号码");
                }
                break;
            case R.id.btn1:
                if(click){
                    linearOne.setVisibility(View.GONE);
                    linearTwo.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.btn2:
                if (click2) {
                    if (password1.equals(password2)) {
                        showProgress("正在请求...");
                        OkSimpleRequest request = new OkSimpleRequest<>(R.id.updatePassword,
                                Constants.updatePassword);
                        //request.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
                        request.addParams("phone", phone);
                        request.addParams("authCode", code);
                        if(type==1){
                            request.addParams("loginPassword", password1);
                        }else if(type == 2){
                            request.addParams("payPassword", password1);
                        }
                        //request.addHeadParams("userType", AppDroid.getInstance().getUserOtherBean().getUserType());
                        //request.addHeadParams("userToken", AppDroid.getInstance().getUserOtherBean().getToken());
                        requestOkhttpSimple(request);
                    } else {
                        showToast("两次输入的密码不同");
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.authPhone:
                //showToast(t.getDesc());
                break;
            case R.id.updatePassword:
                showToast(t.getDesc());
                hideProgress();
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.authPhone:
                showToast(t.getDesc());
                break;
            case R.id.updatePassword:
                hideProgress();
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    private void clickOne() {
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            click = false;
            btn1.setBackgroundResource(R.drawable.shape_one_normal);
        } else {
            click = true;
            btn1.setBackgroundResource(R.drawable.shape_one_press);
        }
    }

    private void clickTwo() {
        if (TextUtils.isEmpty(password1) || TextUtils.isEmpty(password1)) {
            click2 = false;
            btn2.setBackgroundResource(R.drawable.shape_one_normal);
        } else {
            click2 = true;
            btn2.setBackgroundResource(R.drawable.shape_one_press);
        }
    }
}
